import { proxy, reqHeader } from '../../utils/util.js';
import LoginStore from '../../pages/login/store.js';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '', //导航栏 中间的标题
      background: 'linear-gradient(to bottom, rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0))',
      hasFilter: true,
      color: '#FFFFFF',
      btnStyle: 'white', //black, white
    },
    navBarHeight: app.globalData.height,
    changyongList:[],
    searchKey:'',
    searchList:[],
    page:0,
    size:20,
    hasNext:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let me = this;
    wx.request({
      url: `${proxy}/community/follow/users`,
      data: {
        userId: LoginStore.userInfo.userId,
        listType:"friend",
        nickName: "",
        access_token: LoginStore.userInfo.access_token
      },
      header: reqHeader,
      success: function (res) {
        res = res.data;
        if(res.status == 200){
          me.setData({
            changyongList:res.data
          })
        }else{
          console.log(res.message)
        }
      }
    });
  },
  searchFun:function(key,page){
    let me = this;
    wx.request({
      url: `${proxy}/community/searchv2`,
      data: {
        key: key,
        searchType: "user",
        imgSize: "p2,p4",
        page: page,
        size: this.data.size,
        access_token: LoginStore.userInfo.access_token
      },
      header: reqHeader,
      success: function (res) {
        res = res.data;
        if (res.status == 200) {
          let hasNext = true;
          if (!res.data.length || res.data.length < me.data.size) {
            hasNext = false;
          }
          let list = me.data.searchList.concat(res.data);
          me.setData({
            searchList: list,
            hasNext: hasNext,
            page: page
          })
        } else {
          console.log(res.message)
        }
      }
    });
  },
  handlerSearch:function(e){
    let v = e.detail.value;
    this.setData({
      searchKey:v
    })
    this.searchFun(v,1);
  },
  selectAtPeople:function(e){
    let data = e.currentTarget.dataset;
    let detail = data.detail;

    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    let commentCmp = prevPage.selectComponent('#commentsCmp');
    commentCmp.addAtPeople(detail,function(){
      wx.navigateBack({
        delta: 1
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.hasNext){
      this.searchFun(this.data.searchKey, this.data.page + 1);
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})